package com.example.graphic.model.vo;

import com.example.graphic.constant.ErrorCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
public class BaseResultVO<T> {
    private Integer code;
    private String msg;
    private T data;

    public BaseResultVO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public BaseResultVO(Integer code, String msg,T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功且无数据
     */
    public static <T> BaseResultVO<T> success() {
        return success(null);
    }

    /**
     * 成功并返回数据
     */
    public static <T> BaseResultVO<T> success(T data) {
        return new BaseResultVO<>(ErrorCodeEnum.SUCCESS.getCode(), "success", data);
    }

    /**
     * 自定义成功信息
     */
    public static <T> BaseResultVO<T> success(String message, T data) {
        return new BaseResultVO<>(ErrorCodeEnum.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败，使用默认错误码 REQUEST_FAIL
     */
    public static <T> BaseResultVO<T> fail(String message) {
        return new BaseResultVO<>(ErrorCodeEnum.REQUEST_FAIL.getCode(), message);
    }

    /**
     * 失败，使用指定错误码
     */
    public static <T> BaseResultVO<T> fail(ErrorCodeEnum error) {
        return new BaseResultVO<>(error.getCode(), error.name());
    }

    /**
     * 链式设置 data
     */
    public BaseResultVO<T> withData(T data) {
        this.setData(data);
        return this;
    }
}
